package com.bestcem.xm.deliver.grpc.client;

import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.member.grpc.v1.services.ProjectServiceGrpc;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class QdesGrpcClient {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BaseQdesServiceGrpc.BaseQdesServiceBlockingStub baseQdesServiceBlockingStub;

    @GrpcClient(GrpcClientNames.DS_QDES)
    private ProjectServiceGrpc.ProjectServiceBlockingStub projectServiceBlockingStub;

    public List<com.bestcem.xm.qdes.grpc.v1.services.Project> ListProjectsByIds(List<String> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            log.error("[deliver] qdes ListProjectsByIds ids 为空");
            return new ArrayList<>();
        }
        ListProjectByIdsRequest.Builder builder = ListProjectByIdsRequest.newBuilder();
        builder.addAllIds(ids);
        try {
            ProjectSearchResponse response = baseQdesServiceBlockingStub.listProjectByIds(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver]: 查询Project失败 ids  {} Error; ErrorMsg: {}", ids, response);
                return new ArrayList<>();
            }
            return response.getProjectListList();
        } catch (Exception e) {
            log.error("[deliver]: 查询Project失败 ids  {} Error", ids, e);
            return new ArrayList<>();
        }
    }

    public Project getProject(String id) {
        GetProjectRequest.Builder builder = GetProjectRequest.newBuilder();
        builder.setId(id);
        try {
            ProjectResponse response = baseQdesServiceBlockingStub.getProject(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] getProject, id = {} Error; ErrorMsg: {}", id, response);
                return null;
            }
            return response.getProject();
        } catch (Exception e) {
            log.error("[deliver] exp getProject {} Error; ErrorMsg: {}", id, e.getMessage(), e);
            return null;
        }
    }

    //public List<ProjectEntry> listProjectEntryByIds(List<String> ids) {
    //    if (ObjectUtils.isEmpty(ids)) {
    //        log.error("[deliver]: 查询listProjectEntryByIds ids 必须");
    //        return new ArrayList<>();
    //    }
    //    ListProjectEntryByIdsRequest.Builder builder = ListProjectEntryByIdsRequest.newBuilder();
    //    builder.addAllIds(ids);
    //    try {
    //        ProjectEntrySearchResponse response = baseQdesServiceBlockingStub.listProjectEntryByIds(builder.build());
    //        if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
    //            log.error("[deliver]: 查询listProjectEntryByIds失败 ids  {} Error; ErrorMsg: {}", ids, response);
    //            return new ArrayList<>();
    //        }
    //        return response.getProjectEntryListList();
    //    } catch (Exception e) {
    //        log.error("[deliver]: 查询listProjectEntryByIds失败 ids  {} Error", ids, e);
    //        return new ArrayList<>();
    //    }
    //}

    public List<ProjectEntry> listProjectEntry(String userId) {
        ListProjectEntryRequest.Builder builder = ListProjectEntryRequest.newBuilder();
        ProjectEntry.Builder entryBuilder = ProjectEntry.newBuilder();
        entryBuilder.setUserId(userId);
        builder.setProjectEntry(entryBuilder.build());
        ListProjectEntryRequest request = builder.build();
        try {
            ProjectEntrySearchResponse response = baseQdesServiceBlockingStub.listProjectEntry(request);
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver]: 查询listProjectEntry失败 ids  {} Error; ErrorMsg: {}", userId, response);
                return new ArrayList<>();
            }
            return response.getProjectEntryListList();
        } catch (Exception e) {
            log.error("[deliver]: 查询listProjectEntry失败 ids  {} Error", userId, e);
            return new ArrayList<>();
        }
    }

    public List<Project> listProjectNoDefault(ProjectNoDefault projectNoDefault) {
        // 因貌似不支持search参数，所以将orgId组织到project中
        ListProjectRequestNoDefault listProjectRequest = ListProjectRequestNoDefault.newBuilder().setProject(projectNoDefault).build();
        ProjectSearchResponse response = baseQdesServiceBlockingStub.listProjectNoDefault(listProjectRequest);
        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
            log.error("【联系人】listProject failed, response: {}", response);
            return Collections.emptyList();
        }
        return response.getProjectListList();
    }


    public QStruct getQStruct(String id) {
        GetQStructRequest request = GetQStructRequest.newBuilder().setId(id).build();
        QStructResponse response = baseQdesServiceBlockingStub.getQStruct(request);
        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
            log.error("【联系人】listProject failed, response: {}", response);
            return null;
        }
        return response.getQStruct();
    }

    public List<String> getProjectIds(String orgId, List<Integer> status) {
        GetProjectIdListRequest request = GetProjectIdListRequest.newBuilder().setOrgId(orgId).addAllStatus(status).build();
        GetProjectIdListResponse response = baseQdesServiceBlockingStub.getProjectIdList(request);
        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
            log.error("【投放】getProjectIds failed, response: {}", response);
            return null;
        }
        return new ArrayList<>(response.getProjectIdListList());
    }
}
